package com.markedquilt.SwordForOffer;

import com.markedquilt.SwordForOffer.entity.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Title:
 * Project: LeetcodeProblem
 *
 * @author ZhangKe
 * Created on 2020/11/30
 */
public class _levelOrder32 {

    public static int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        ArrayList<Integer> array = new ArrayList();
        if (root==null){
            return new int[]{};
        }else{
            queue.add(root);
            while(!queue.isEmpty()){
                TreeNode node = queue.poll();
                array.add(node.val);
                if (node.left!=null){
                    queue.add(node.left);
                }
                if (node.right!=null){
                    queue.add(node.right);
                }
            }
            int[] result = new int[array.size()];
            for (int i = 0; i < array.size(); i++) {
                if (array.get(i)!=null){
                    result[i] = array.get(i);
                }
            }
            return result;
        }
    }
    public static void main(String[] args) {
        TreeNode three = new TreeNode(3);
        TreeNode two = new TreeNode(2);
        TreeNode five = new TreeNode(5);
        TreeNode zero = new TreeNode(0);
        TreeNode one = new TreeNode(1);
        two.left = zero;
        two.right = one;
        three.left = two;
        three.right = five;
        int[] ints = levelOrder(three);
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i]+"  ");
        }
    }
}
